package kr.hkit.powerjava.coffee;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import kr.hkit.powerjava.coffee.common.ConnectionFactory;
import kr.hkit.powerjava.coffee.common.DbUtil;
import kr.hkit.powerjava.coffee.dto.Product;
import kr.hkit.powerjava.coffee.service.ProductService;

public class Main extends JFrame implements ActionListener {

	Connection connection = ConnectionFactory.getConnection();
	ProductService pSvc = new ProductService(connection);

	PreparedStatement pstmt;
	String sql;
	ResultSet rs;

	private JPanel contentPane;
	private JPanel inputPanel;
	private JPanel buttonPanel;
	private JLabel lbl01;
	private JLabel lbl02;
	private JTextField tfPname;
	private JComboBox<String> cbCode;
	private JLabel lbl03;
	private JTextField tfPrice;
	private JLabel lbl04;
	private JTextField tfAmount;
	private JLabel lbl05;
	private JTextField tfMargin;
	private JLabel lblNewLabel;
	private JLabel lblNewLabel_1;
	private JLabel lblNewLabel_2;
	private JLabel lblNewLabel_3;
	private JButton btnInsert;
	private JButton btnPrn1;
	private JButton btnPrn2;

	public static void main(String[] args) {

		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Main frame = new Main();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public Main() throws SQLException {
		initialize();
	}

	private void initialize() throws SQLException {

		setTitle("판매실적 계산");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);

		inputPanel = new JPanel();
		contentPane.add(inputPanel, BorderLayout.CENTER);
		inputPanel.setLayout(new GridLayout(0, 4, 0, 0));

		lbl01 = new JLabel("제품코드");
		inputPanel.add(lbl01);

		cbCode = new JComboBox<String>();
		inputCode(pstmt, rs);
		cbCode.addActionListener(this);
		inputPanel.add(cbCode);

		lbl02 = new JLabel("제품명");
		inputPanel.add(lbl02);

		tfPname = new JTextField();
		tfPname.setEditable(false);
		inputPanel.add(tfPname);
		tfPname.setColumns(10);

		lbl03 = new JLabel("제품단가");
		inputPanel.add(lbl03);

		tfPrice = new JTextField();
		inputPanel.add(tfPrice);
		tfPrice.setColumns(8);

		lblNewLabel = new JLabel("");
		inputPanel.add(lblNewLabel);

		lblNewLabel_1 = new JLabel("");
		inputPanel.add(lblNewLabel_1);

		lbl04 = new JLabel("판매수량");
		inputPanel.add(lbl04);

		tfAmount = new JTextField();
		inputPanel.add(tfAmount);
		tfAmount.setColumns(8);

		lblNewLabel_2 = new JLabel("");
		inputPanel.add(lblNewLabel_2);

		lblNewLabel_3 = new JLabel("");
		inputPanel.add(lblNewLabel_3);

		lbl05 = new JLabel("마 진 율");
		inputPanel.add(lbl05);

		tfMargin = new JTextField();
		inputPanel.add(tfMargin);
		tfMargin.setColumns(2);

		buttonPanel = new JPanel();
		FlowLayout flowLayout = (FlowLayout) buttonPanel.getLayout();
		flowLayout.setHgap(50);
		contentPane.add(buttonPanel, BorderLayout.SOUTH);

		btnInsert = new JButton("입력");
		btnInsert.addActionListener(this);
		buttonPanel.add(btnInsert);

		btnPrn1 = new JButton("출력1");
		btnPrn1.addActionListener(this);
		buttonPanel.add(btnPrn1);

		btnPrn2 = new JButton("출력2");
		buttonPanel.add(btnPrn2);

		DbUtil.close(pstmt);
		DbUtil.close(rs);

	}

	private void inputCode(PreparedStatement pstmt, ResultSet rs)
			throws SQLException {
		sql = "select code, name from code";
		pstmt = connection.prepareStatement(sql);
		rs = pstmt.executeQuery();
		while (rs.next()) {
			cbCode.addItem(rs.getString(1));
		}
	}

	public void actionPerformed(ActionEvent e) {
		try {
			if (e.getSource() == btnPrn1) {
				actionPerformedBtnPrn1(e);
			}
			if (e.getSource() == btnInsert) {
				actionPerformedBtnInsert(e);
			}
			if (e.getSource() == cbCode) {
				actionPerformedComboBox(e);
			}
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
	}

	protected void actionPerformedComboBox(ActionEvent e) throws SQLException {
		sql = "select name from code where code = ?";
		pstmt = connection.prepareStatement(sql);
		pstmt.setString(1, (String) cbCode.getSelectedItem());
		rs = pstmt.executeQuery();
		if (rs.next()) {
			tfPname.setText(rs.getString(1));
		}
	}

	protected void actionPerformedBtnInsert(ActionEvent e) throws SQLException {
		try {
			pSvc.insertItem(new Product((String) cbCode.getSelectedItem(),
					Integer.parseInt(tfPrice.getText()), Integer
							.parseInt(tfAmount.getText()), Integer
							.parseInt(tfMargin.getText())));
		} catch (NumberFormatException e1) {
			JOptionPane.showMessageDialog(this, "잘못된 값");
		} finally {
			cbCode.requestFocus();
			cbCode.setSelectedItem(null);
			tfPname.setText("");
			tfPrice.setText("");
			tfAmount.setText("");
			tfMargin.setText("");
		}
	}

	protected void actionPerformedBtnPrn1(ActionEvent e) throws SQLException {
		sql = "select c.name, p.p_code, format(price, 0), format(amount, 0), format(supply, 0), format(surtax, 0), format(sellingprice, 0), margin, format(marginsum, 0) from product p, code c where c.code = p.p_code order by p.sellingprice desc";
		pstmt = connection.prepareStatement(sql);
		rs = pstmt.executeQuery();
		new Prn1(connection, rs);
	}

}
